package com.jetlion.contentprovider.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jetlion.contentprovider.constants.Constants;
import com.jetlion.contentprovider.db.DBOpenHelper;


public class CustomProvider extends ContentProvider {

    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int STUDENT_ALL = 0;

    private static final int STUDENT_ITEM = 1;

    private SQLiteDatabase database;

    static {
        matcher.addURI(Constants.AUTHORITY, "student", STUDENT_ALL);
        matcher.addURI(Constants.AUTHORITY, "students/#", STUDENT_ITEM);
    }


    @Override
    public boolean onCreate() {

        //实例化数据库对象
        DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext(), "student.db", 1);
        database = dbOpenHelper.getWritableDatabase();
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] columns, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String orderBy) {

        Cursor cursor = null;

        //解析uri
        switch (matcher.match(uri)) {
            case STUDENT_ALL:
                cursor = database.query("student", columns, selection, selectionArgs, null, null, orderBy);
                break;
            case STUDENT_ITEM:
                long id = ContentUris.parseId(uri);
                String str = Constants._ID + "+" + id;
                if (!TextUtils.isEmpty(selection)) {
                    selection = selection + "and" + str;
                }
                cursor = database.query("student", columns, selection, selectionArgs, null, null, orderBy);
                break;
            default:
                break;
        }

        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {

        String result = null;

        //解析uri
        switch (matcher.match(uri)) {

            case STUDENT_ALL:
                result = Constants.DATA_TYPE;
                break;
            case STUDENT_ITEM:
                result = Constants.DATA_TYPE_ITEM;
                break;
            default:
                break;
        }
        return result;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {

        long rowId = database.insert("student", Constants._ID, contentValues);

        if (rowId > 0) {
            //Uri追加新行的Id值
            Uri newRowUri = ContentUris.withAppendedId(uri, rowId);

            //通知发生改变
            getContext().getContentResolver().notifyChange(newRowUri, null);
            return newRowUri;
        }

        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {

        int num = 0;

        //解析uri
        switch (matcher.match(uri)) {

            case STUDENT_ALL:
                num = database.delete("student", selection, selectionArgs);
                break;

            case STUDENT_ITEM:
                long id = ContentUris.parseId(uri);
                String str = Constants._ID + "=" + id;
                if (!TextUtils.isEmpty(str)) {
                    selection = selection + "and" + str;
                }
                num = database.delete("student", selection, selectionArgs);
                break;
            default:
                break;

        }
        //通知发生改变
        getContext().getContentResolver().notifyChange(uri, null);

        return num;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String selection, @Nullable String[] selectionArgs) {

        int num = 0;

        switch (matcher.match(uri)) {
            case STUDENT_ALL:
                num = database.update("student", contentValues, selection, selectionArgs);
                break;
            case STUDENT_ITEM:
                long id = ContentUris.parseId(uri);
                String str = Constants._ID + "=" + id;
                if (!TextUtils.isEmpty(str)) {
                    selection = selection + "and" + str;
                }
                num = database.update("student", contentValues, selection, selectionArgs);
                break;
            default:
                break;
        }
        getContext().getContentResolver().notifyChange(uri, null);

        return num;
    }
}
